package main

import (
	"encoding/json"
	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/ledger"
	ledger2 "jlpay-sdk-go/pkg/jlpay/services/ledger"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// 余额分账示例
func balanceSplitDemo() {
	config := demo.CreateDemoConfig()
	balanceSplitService := ledger2.NewBalanceSplitService(config)

	// 创建余额分账请求
	request := ledger.NewBalanceSplitRequest()

	// 创建接收方列表
	receivers := []map[string]interface{}{
		{
			"rev_mch_id":  "849584307420004",
			"amount":      500,
			"description": "分账给接收方1",
		},
		{
			"rev_mch_id":  "849602058120001",
			"amount":      500,
			"description": "分账给接收方2",
		},
	}

	// 直接使用map构建请求
	requestMap := map[string]interface{}{
		"org_code":     "50720711",
		"mch_id":       "849581183510017",
		"out_order_no": "045e1314f7784f46bf9d590dc65c7",
		"total_amount": 1000,
		"receivers":    receivers,
	}

	// 设置原始请求数据
	request.SetRawRequestMap(requestMap)

	// 序列化请求数据并打印
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("余额分账请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := balanceSplitService.SplitBalance(request)
	if err != nil {
		utils.Errorf("余额分账请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("余额分账")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 余额分账示例 开始 =====")
	utils.Info("=============================")

	balanceSplitDemo()

	utils.Info("=============================")
	utils.Info("===== 余额分账示例执行完毕 =====")
	utils.Info("=============================")
}
